home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 44 / PC Actual CD 44.iso / Linux / Cygwin / full.exe / Disk1 / data1.cab / Tools / share / tcl8.0 / init.tcl < prev    next >
Encoding:
Text File  |  1998-12-04  |  23.6 KB  |  792 lines

  1. # init.tcl --
  2. #
  3. # Default system startup file for Tcl-based applications.  Defines
  4. # "unknown" procedure and auto-load facilities.
  5. #
  6. # SCCS: @(#) init.tcl 1.95 97/11/19 17:16:34
  7. #
  8. # Copyright (c) 1991-1993 The Regents of the University of California.
  9. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14.  
  15. if {[info commands package] == ""} {
  16.     error "version mismatch: library\nscripts expect Tcl version 7.5b1 or later but the loaded version is\nonly [info patchlevel]"
  17. }
  18. package require -exact Tcl 8.0
  19.  
  20. # Compute the auto path to use in this interpreter.
  21. # (auto_path could be already set, in safe interps for instance)
  22.  
  23. if {![info exists auto_path]} {
  24.     if [catch {set auto_path $env(TCLLIBPATH)}] {
  25.     set auto_path ""
  26.     }
  27. }
  28. if {[lsearch -exact $auto_path [info library]] < 0} {
  29.     lappend auto_path [info library]
  30. }
  31. catch {
  32.     foreach __dir $tcl_pkgPath {
  33.     if {[lsearch -exact $auto_path $__dir] < 0} {
  34.         lappend auto_path $__dir
  35.     }
  36.     }
  37.     unset __dir
  38. }
  39.  
  40. # Setup the unknown package handler
  41.  
  42. package unknown tclPkgUnknown
  43.  
  44. # Conditionalize for presence of exec.
  45.  
  46. if {[info commands exec] == ""} {
  47.  
  48.     # Some machines, such as the Macintosh, do not have exec. Also, on all
  49.     # platforms, safe interpreters do not have exec.
  50.  
  51.     set auto_noexec 1
  52. }
  53. set errorCode ""
  54. set errorInfo ""
  55.  
  56. # Define a log command (which can be overwitten to log errors
  57. # differently, specially when stderr is not available)
  58.  
  59. if {[info commands tclLog] == ""} {
  60.     proc tclLog {string} {
  61.     catch {puts stderr $string}
  62.     }
  63. }
  64.  
  65. # The procs defined in this file that have a leading space
  66. # are 'hidden' from auto_mkindex because they are not
  67. # auto-loadable.
  68.  
  69.  
  70. # unknown --
  71. # This procedure is called when a Tcl command is invoked that doesn't
  72. # exist in the interpreter.  It takes the following steps to make the
  73. # command available:
  74. #
  75. #    1. See if the autoload facility can locate the command in a
  76. #       Tcl script file.  If so, load it and execute it.
  77. #    2. If the command was invoked interactively at top-level:
  78. #        (a) see if the command exists as an executable UNIX program.
  79. #        If so, "exec" the command.
  80. #        (b) see if the command requests csh-like history substitution
  81. #        in one of the common forms !!, !<number>, or ^old^new.  If
  82. #        so, emulate csh's history substitution.
  83. #        (c) see if the command is a unique abbreviation for another
  84. #        command.  If so, invoke the command.
  85. #
  86. # Arguments:
  87. # args -    A list whose elements are the words of the original
  88. #        command, including the command name.
  89.  
  90.  proc unknown args {
  91.     global auto_noexec auto_noload env unknown_pending tcl_interactive
  92.     global errorCode errorInfo
  93.  
  94.     # Save the values of errorCode and errorInfo variables, since they
  95.     # may get modified if caught errors occur below.  The variables will
  96.     # be restored just before re-executing the missing command.
  97.  
  98.     set savedErrorCode $errorCode
  99.     set savedErrorInfo $errorInfo
  100.     set name [lindex $args 0]
  101.     if ![info exists auto_noload] {
  102.     #
  103.     # Make sure we're not trying to load the same proc twice.
  104.     #
  105.     if [info exists unknown_pending($name)] {
  106.         return -code error "self-referential recursion in \"unknown\" for command \"$name\"";
  107.     }
  108.     set unknown_pending($name) pending;
  109.     set ret [catch {auto_load $name [uplevel 1 {namespace current}]} msg]
  110.     unset unknown_pending($name);
  111.     if {$ret != 0} {
  112.         return -code $ret -errorcode $errorCode \
  113.         "error while autoloading \"$name\": $msg"
  114.     }
  115.     if ![array size unknown_pending] {
  116.         unset unknown_pending
  117.     }
  118.     if $msg {
  119.         set errorCode $savedErrorCode
  120.         set errorInfo $savedErrorInfo
  121.         set code [catch {uplevel 1 $args} msg]
  122.         if {$code ==  1} {
  123.         #
  124.         # Strip the last five lines off the error stack (they're
  125.         # from the "uplevel" command).
  126.         #
  127.  
  128.         set new [split $errorInfo \n]
  129.         set new [join [lrange $new 0 [expr [llength $new] - 6]] \n]
  130.         return -code error -errorcode $errorCode \
  131.             -errorinfo $new $msg
  132.         } else {
  133.         return -code $code $msg
  134.         }
  135.     }
  136.     }
  137.  
  138.     if {([info level] == 1) && ([info script] == "") \
  139.         && [info exists tcl_interactive] && $tcl_interactive} {
  140.     if ![info exists auto_noexec] {
  141.         set new [auto_execok $name]
  142.         if {$new != ""} {
  143.         set errorCode $savedErrorCode
  144.         set errorInfo $savedErrorInfo
  145.         set redir ""
  146.         if {[info commands console] == ""} {
  147.             set redir ">&@stdout <@stdin"
  148.         }
  149.         return [uplevel exec $redir $new [lrange $args 1 end]]
  150.         }
  151.     }
  152.     set errorCode $savedErrorCode
  153.     set errorInfo $savedErrorInfo
  154.     if {$name == "!!"} {
  155.         set newcmd [history event]
  156.     } elseif {[regexp {^!(.+)$} $name dummy event]} {
  157.         set newcmd [history event $event]
  158.     } elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $name dummy old new]} {
  159.         set newcmd [history event -1]
  160.         catch {regsub -all -- $old $newcmd $new newcmd}
  161.     }
  162.     if [info exists newcmd] {
  163.         tclLog $newcmd
  164.         history change $newcmd 0
  165.         return [uplevel $newcmd]
  166.     }
  167.  
  168.     set ret [catch {set cmds [info commands $name*]} msg]
  169.     if {[string compare $name "::"] == 0} {
  170.         set name ""
  171.     }
  172.     if {$ret != 0} {
  173.         return -code $ret -errorcode $errorCode \
  174.         "error in unknown while checking if \"$name\" is a unique command abbreviation: $msg"
  175.     }
  176.     if {[llength $cmds] == 1} {
  177.         return [uplevel [lreplace $args 0 0 $cmds]]
  178.     }
  179.     if {[llength $cmds] != 0} {
  180.         if {$name == ""} {
  181.         return -code error "empty command name \"\""
  182.         } else {
  183.         return -code error \
  184.             "ambiguous command name \"$name\": [lsort $cmds]"
  185.         }
  186.     }
  187.     }
  188.     return -code error "invalid command name \"$name\""
  189. }
  190.  
  191. # auto_load --
  192. # Checks a collection of library directories to see if a procedure
  193. # is defined in one of them.  If so, it sources the appropriate
  194. # library file to create the procedure.  Returns 1 if it successfully
  195. # loaded the procedure, 0 otherwise.
  196. #
  197. # Arguments: 
  198. # cmd -            Name of the command to find and load.
  199. # namespace (optional)  The namespace where the command is being used - must be
  200. #                       a canonical namespace as returned [namespace current]
  201. #                       for instance. If not given, namespace current is used.
  202.  
  203.  proc auto_load {cmd {namespace {}}} {
  204.     global auto_index auto_oldpath auto_path env errorInfo errorCode
  205.  
  206.     if {[string length $namespace] == 0} {
  207.     set namespace [uplevel {namespace current}]
  208.     }
  209.     set nameList [auto_qualify $cmd $namespace]
  210.     # workaround non canonical auto_index entries that might be around
  211.     # from older auto_mkindex versions
  212.     lappend nameList $cmd
  213.     foreach name $nameList {
  214.     if [info exists auto_index($name)] {
  215.         uplevel #0 $auto_index($name)
  216.         return [expr {[info commands $name] != ""}]
  217.     }
  218.     }
  219.     if ![info exists auto_path] {
  220.     return 0
  221.     }
  222.     if [info exists auto_oldpath] {
  223.     if {$auto_oldpath == $auto_path} {
  224.         return 0
  225.     }
  226.     }
  227.     set auto_oldpath $auto_path
  228.  
  229.     # Check if we are a safe interpreter. In that case, we support only
  230.     # newer format tclIndex files.
  231.  
  232.     set issafe [interp issafe]
  233.     for {set i [expr [llength $auto_path] - 1]} {$i >= 0} {incr i -1} {
  234.     set dir [lindex $auto_path $i]
  235.     set f ""
  236.     if {$issafe} {
  237.         catch {source [file join $dir tclIndex]}
  238.     } elseif [catch {set f [open [file join $dir tclIndex]]}] {
  239.         continue
  240.     } else {
  241.         set error [catch {
  242.         set id [gets $f]
  243.         if {$id == "# Tcl autoload index file, version 2.0"} {
  244.             eval [read $f]
  245.         } elseif {$id == \
  246.             "# Tcl autoload index file: each line identifies a Tcl"} {
  247.             while {[gets $f line] >= 0} {
  248.             if {([string index $line 0] == "#")
  249.                 || ([llength $line] != 2)} {
  250.                 continue
  251.             }
  252.             set name [lindex $line 0]
  253.             set auto_index($name) \
  254.                 "source [file join $dir [lindex $line 1]]"
  255.             }
  256.         } else {
  257.             error \
  258.               "[file join $dir tclIndex] isn't a proper Tcl index file"
  259.         }
  260.         } msg]
  261.         if {$f != ""} {
  262.         close $f
  263.         }
  264.         if $error {
  265.         error $msg $errorInfo $errorCode
  266.         }
  267.     }
  268.     }
  269.     foreach name $nameList {
  270.     if [info exists auto_index($name)] {
  271.         uplevel #0 $auto_index($name)
  272.         if {[info commands $name] != ""} {
  273.         return 1
  274.         }
  275.     }
  276.     }
  277.     return 0
  278. }
  279.  
  280. # auto_qualify --
  281. # compute a fully qualified names list for use in the auto_index array.
  282. # For historical reasons, commands in the global namespace do not have leading
  283. # :: in the index key. The list has two elements when the command name is
  284. # relative (no leading ::) and the namespace is not the global one. Otherwise
  285. # only one name is returned (and searched in the auto_index).
  286. #
  287. # Arguments -
  288. # cmd        The command name. Can be any name accepted for command
  289. #               invocations (Like "foo::::bar").
  290. # namespace    The namespace where the command is being used - must be
  291. #               a canonical namespace as returned by [namespace current]
  292. #               for instance.
  293.  
  294.  proc auto_qualify {cmd namespace} {
  295.  
  296.     # count separators and clean them up
  297.     # (making sure that foo:::::bar will be treated as foo::bar)
  298.     set n [regsub -all {::+} $cmd :: cmd]
  299.  
  300.     # Ignore namespace if the name starts with ::
  301.     # Handle special case of only leading ::
  302.  
  303.     # Before each return case we give an example of which category it is
  304.     # with the following form :
  305.     # ( inputCmd, inputNameSpace) -> output
  306.  
  307.     if {[regexp {^::(.*)$} $cmd x tail]} {
  308.     if {$n > 1} {
  309.         # ( ::foo::bar , * ) -> ::foo::bar
  310.         return [list $cmd]
  311.     } else {
  312.         # ( ::global , * ) -> global
  313.         return [list $tail]
  314.     }
  315.     }
  316.     
  317.     # Potentially returning 2 elements to try  :
  318.     # (if the current namespace is not the global one)
  319.  
  320.     if {$n == 0} {
  321.     if {[string compare $namespace ::] == 0} {
  322.         # ( nocolons , :: ) -> nocolons
  323.         return [list $cmd]
  324.     } else {
  325.         # ( nocolons , ::sub ) -> ::sub::nocolons nocolons
  326.         return [list ${namespace}::$cmd $cmd]
  327.     }
  328.     } else {
  329.     if {[string compare $namespace ::] == 0} {
  330.         #  ( foo::bar , :: ) -> ::foo::bar
  331.         return [list ::$cmd]
  332.     } else {
  333.         # ( foo::bar , ::sub ) -> ::sub::foo::bar ::foo::bar
  334.         return [list ${namespace}::$cmd ::$cmd]
  335.     }
  336.     }
  337. }
  338.  
  339. if {[string compare $tcl_platform(platform) windows] == 0} {
  340.  
  341. # auto_execok --
  342. #
  343. # Returns string that indicates name of program to execute if 
  344. # name corresponds to a shell builtin or an executable in the
  345. # Windows search path, or "" otherwise.  Builds an associative 
  346. # array auto_execs that caches information about previous checks, 
  347. # for speed.
  348. #
  349. # Arguments: 
  350. # name -            Name of a command.
  351.  
  352. # Windows version.
  353. #
  354. # Note that info executable doesn't work under Windows, so we have to
  355. # look for files with .exe, .com, or .bat extensions.  Also, the path
  356. # may be in the Path or PATH environment variables, and path
  357. # components are separated with semicolons, not colons as under Unix.
  358. #
  359. proc auto_execok name {
  360.     global auto_execs env tcl_platform
  361.  
  362.     if [info exists auto_execs($name)] {
  363.     return $auto_execs($name)
  364.     }
  365.     set auto_execs($name) ""
  366.  
  367.     if {[lsearch -exact {cls copy date del erase dir echo mkdir md rename 
  368.         ren rmdir rd time type ver vol} $name] != -1} {
  369.     return [set auto_execs($name) [list $env(COMSPEC) /c $name]]
  370.     }
  371.  
  372.     if {[llength [file split $name]] != 1} {
  373.     foreach ext {{} .com .exe .bat} {
  374.         set file ${name}${ext}
  375.         if {[file exists $file] && ![file isdirectory $file]} {
  376.         return [set auto_execs($name) [list $file]]
  377.         }
  378.     }
  379.     return ""
  380.     }
  381.  
  382.     set path "[file dirname [info nameof]];.;"
  383.     if {[info exists env(WINDIR)]} {
  384.     set windir $env(WINDIR) 
  385.     }
  386.     if {[info exists windir]} {
  387.     if {$tcl_platform(os) == "Windows NT"} {
  388.         append path "$windir/system32;"
  389.     }
  390.     append path "$windir/system;$windir;"
  391.     }
  392.  
  393.     if {[info exists env(PATH)]} {
  394.         # CYGNUS LOCAL: in the Cygwin environment, we convert to a
  395.     # Windows path first.
  396.         if {[llength [info commands ide_cygwin_path]]} {
  397.         append path [ide_cygwin_path posix_to_win32_path_list $env(PATH)]
  398.     } else {
  399.         append path $env(PATH)
  400.     }
  401.     }
  402.  
  403.     foreach dir [split $path {;}] {
  404.     if {$dir == ""} {
  405.         set dir .
  406.     }
  407.     foreach ext {{} .com .exe .bat} {
  408.         set file [file join $dir ${name}${ext}]
  409.         if {[file exists $file] && ![file isdirectory $file]} {
  410.         return [set auto_execs($name) [list $file]]
  411.         }
  412.     }
  413.     }
  414.     return ""
  415. }
  416.  
  417. } else {
  418.  
  419. # auto_execok --
  420. #
  421. # Returns string that indicates name of program to execute if 
  422. # name corresponds to an executable in the path. Builds an associative 
  423. # array auto_execs that caches information about previous checks, 
  424. # for speed.
  425. #
  426. # Arguments: 
  427. # name -            Name of a command.
  428.  
  429. # Unix version.
  430. #
  431. proc auto_execok name {
  432.     global auto_execs env
  433.  
  434.     if [info exists auto_execs($name)] {
  435.     return $auto_execs($name)
  436.     }
  437.     set auto_execs($name) ""
  438.     if {[llength [file split $name]] != 1} {
  439.     if {[file executable $name] && ![file isdirectory $name]} {
  440.         set auto_execs($name) [list $name]
  441.     }
  442.     return $auto_execs($name)
  443.     }
  444.     foreach dir [split $env(PATH) :] {
  445.     if {$dir == ""} {
  446.         set dir .
  447.     }
  448.     set file [file join $dir $name]
  449.     if {[file executable $file] && ![file isdirectory $file]} {
  450.         set auto_execs($name) [list $file]
  451.         return $auto_execs($name)
  452.     }
  453.     }
  454.     return ""
  455. }
  456.  
  457. }
  458. # auto_reset --
  459. # Destroy all cached information for auto-loading and auto-execution,
  460. # so that the information gets recomputed the next time it's needed.
  461. # Also delete any procedures that are listed in the auto-load index
  462. # except those defined in this file.
  463. #
  464. # Arguments: 
  465. # None.
  466.  
  467. proc auto_reset {} {
  468.     global auto_execs auto_index auto_oldpath
  469.     foreach p [info procs] {
  470.     if {[info exists auto_index($p)] && ![string match auto_* $p]
  471.         && ([lsearch -exact {unknown pkg_mkIndex tclPkgSetup
  472.             tclMacPkgSearch tclPkgUnknown} $p] < 0)} {
  473.         rename $p {}
  474.     }
  475.     }
  476.     catch {unset auto_execs}
  477.     catch {unset auto_index}
  478.     catch {unset auto_oldpath}
  479. }
  480.  
  481. # auto_mkindex --
  482. # Regenerate a tclIndex file from Tcl source files.  Takes as argument
  483. # the name of the directory in which the tclIndex file is to be placed,
  484. # followed by any number of glob patterns to use in that directory to
  485. # locate all of the relevant files. It does not parse or source the file
  486. # so the generated index will not contain the appropriate namespace qualifiers
  487. # if you don't explicitly specify it.
  488. #
  489. # Arguments: 
  490. # dir -            Name of the directory in which to create an index.
  491. # args -        Any number of additional arguments giving the
  492. #            names of files within dir.  If no additional
  493. #            are given auto_mkindex will look for *.tcl.
  494.  
  495. proc auto_mkindex {dir args} {
  496.     global errorCode errorInfo
  497.     set oldDir [pwd]
  498.     cd $dir
  499.     set dir [pwd]
  500.     append index "# Tcl autoload index file, version 2.0\n"
  501.     append index "# This file is generated by the \"auto_mkindex\" command\n"
  502.     append index "# and sourced to set up indexing information for one or\n"
  503.     append index "# more commands.  Typically each line is a command that\n"
  504.     append index "# sets an element in the auto_index array, where the\n"
  505.     append index "# element name is the name of a command and the value is\n"
  506.     append index "# a script that loads the command.\n\n"
  507.     if {$args == ""} {
  508.     set args *.tcl
  509.     }
  510.     foreach file [eval glob $args] {
  511.     set f ""
  512.     set error [catch {
  513.         set f [open $file]
  514.         while {[gets $f line] >= 0} {
  515.         if [regexp {^proc[     ]+([^     ]*)} $line match procName] {
  516.             set procName [lindex [auto_qualify $procName "::"] 0]
  517.             append index "set [list auto_index($procName)]"
  518.             append index " \[list source \[file join \$dir [list $file]\]\]\n"
  519.         }
  520.         }
  521.         close $f
  522.     } msg]
  523.     if $error {
  524.         set code $errorCode
  525.         set info $errorInfo
  526.         catch {close $f}
  527.         cd $oldDir
  528.         error $msg $info $code
  529.     }
  530.     }
  531.     set f ""
  532.     set error [catch {
  533.     set f [open tclIndex w]
  534.     puts $f $index nonewline
  535.     close $f
  536.     cd $oldDir
  537.     } msg]
  538.     if $error {
  539.     set code $errorCode
  540.     set info $errorInfo
  541.     catch {close $f}
  542.     cd $oldDir
  543.     error $msg $info $code
  544.     }
  545. }
  546.  
  547. # pkg_mkIndex --
  548. # This procedure creates a package index in a given directory.  The
  549. # package index consists of a "pkgIndex.tcl" file whose contents are
  550. # a Tcl script that sets up package information with "package require"
  551. # commands.  The commands describe all of the packages defined by the
  552. # files given as arguments.
  553. #
  554. # Arguments:
  555. # dir -            Name of the directory in which to create the index.
  556. # args -        Any number of additional arguments, each giving
  557. #            a glob pattern that matches the names of one or
  558. #            more shared libraries or Tcl script files in
  559. #            dir.
  560.  
  561. proc pkg_mkIndex {dir args} {
  562.     global errorCode errorInfo
  563.     if {[llength $args] == 0} {
  564.     return -code error "wrong # args: should be\
  565.         \"pkg_mkIndex dir pattern ?pattern ...?\"";
  566.     }
  567.     append index "# Tcl package index file, version 1.0\n"
  568.     append index "# This file is generated by the \"pkg_mkIndex\" command\n"
  569.     append index "# and sourced either when an application starts up or\n"
  570.     append index "# by a \"package unknown\" script.  It invokes the\n"
  571.     append index "# \"package ifneeded\" command to set up package-related\n"
  572.     append index "# information so that packages will be loaded automatically\n"
  573.     append index "# in response to \"package require\" commands.  When this\n"
  574.     append index "# script is sourced, the variable \$dir must contain the\n"
  575.     append index "# full path name of this file's directory.\n"
  576.     set oldDir [pwd]
  577.     cd $dir
  578.     foreach file [eval glob $args] {
  579.     # For each file, figure out what commands and packages it provides.
  580.     # To do this, create a child interpreter, load the file into the
  581.     # interpreter, and get a list of the new commands and packages
  582.     # that are defined.  Define an empty "package unknown" script so
  583.     # that there are no recursive package inclusions.
  584.  
  585.     set c [interp create]
  586.  
  587.     # If Tk is loaded in the parent interpreter, load it into the
  588.     # child also, in case the extension depends on it.
  589.  
  590.     foreach pkg [info loaded] {
  591.         if {[lindex $pkg 1] == "Tk"} {
  592.         $c eval {set argv {-geometry +0+0}}
  593.         load [lindex $pkg 0] Tk $c
  594.         break
  595.         }
  596.     }
  597.     $c eval [list set file $file]
  598.     if [catch {
  599.         $c eval {
  600.         proc dummy args {}
  601.         rename package package-orig
  602.         proc package {what args} {
  603.             switch -- $what {
  604.             require { return ; # ignore transitive requires }
  605.             default { eval package-orig {$what} $args }
  606.             }
  607.         }
  608.         proc pkgGetAllNamespaces {{root {}}} {
  609.             set list $root
  610.                     foreach ns [namespace children $root] {
  611.                         eval lappend list [pkgGetAllNamespaces $ns]
  612.                     }
  613.                     return $list
  614.                 }
  615.         package unknown dummy
  616.         set origCmds [info commands]
  617.         set dir ""        ;# in case file is pkgIndex.tcl
  618.         set pkgs ""
  619.  
  620.         # Try to load the file if it has the shared library extension,
  621.         # otherwise source it.  It's important not to try to load
  622.         # files that aren't shared libraries, because on some systems
  623.         # (like SunOS) the loader will abort the whole application
  624.         # when it gets an error.
  625.  
  626.         if {[string compare [file extension $file] \
  627.             [info sharedlibextension]] == 0} {
  628.  
  629.             # The "file join ." command below is necessary.  Without
  630.             # it, if the file name has no \'s and we're on UNIX, the
  631.             # load command will invoke the LD_LIBRARY_PATH search
  632.             # mechanism, which could cause the wrong file to be used.
  633.  
  634.             load [file join . $file]
  635.             set type load
  636.         } else {
  637.             source $file
  638.             set type source
  639.         }
  640.         foreach ns [pkgGetAllNamespaces] {
  641.             namespace import ${ns}::*
  642.         }
  643.         foreach i [info commands] {
  644.             set cmds($i) 1
  645.         }
  646.         foreach i $origCmds {
  647.             catch {unset cmds($i)}
  648.  
  649.         }
  650.         foreach i [array names cmds] {
  651.             # reverse engineer which namespace a command comes from
  652.             set absolute [namespace origin $i]
  653.             if {[string compare ::$i $absolute] != 0} {
  654.             set cmds($absolute) 1
  655.             unset cmds($i)
  656.             }
  657.         }
  658.         foreach i [package names] {
  659.             if {([string compare [package provide $i] ""] != 0)
  660.                 && ([string compare $i Tcl] != 0)
  661.                 && ([string compare $i Tk] != 0)} {
  662.             lappend pkgs [list $i [package provide $i]]
  663.             }
  664.         }
  665.         }
  666.     } msg] {
  667.         tclLog "error while loading or sourcing $file: $msg"
  668.     }
  669.     foreach pkg [$c eval set pkgs] {
  670.         lappend files($pkg) [list $file [$c eval set type] \
  671.             [lsort [$c eval array names cmds]]]
  672.     }
  673.     interp delete $c
  674.     }
  675.     foreach pkg [lsort [array names files]] {
  676.     append index "\npackage ifneeded $pkg\
  677.         \[list tclPkgSetup \$dir [lrange $pkg 0 0] [lrange $pkg 1 1]\
  678.         [list $files($pkg)]\]"
  679.     }
  680.     set f [open pkgIndex.tcl w]
  681.     puts $f $index
  682.     close $f
  683.     cd $oldDir
  684. }
  685.  
  686. # tclPkgSetup --
  687. # This is a utility procedure use by pkgIndex.tcl files.  It is invoked
  688. # as part of a "package ifneeded" script.  It calls "package provide"
  689. # to indicate that a package is available, then sets entries in the
  690. # auto_index array so that the package's files will be auto-loaded when
  691. # the commands are used.
  692. #
  693. # Arguments:
  694. # dir -            Directory containing all the files for this package.
  695. # pkg -            Name of the package (no version number).
  696. # version -        Version number for the package, such as 2.1.3.
  697. # files -        List of files that constitute the package.  Each
  698. #            element is a sub-list with three elements.  The first
  699. #            is the name of a file relative to $dir, the second is
  700. #            "load" or "source", indicating whether the file is a
  701. #            loadable binary or a script to source, and the third
  702. #            is a list of commands defined by this file.
  703.  
  704. proc tclPkgSetup {dir pkg version files} {
  705.     global auto_index
  706.  
  707.     package provide $pkg $version
  708.     foreach fileInfo $files {
  709.     set f [lindex $fileInfo 0]
  710.     set type [lindex $fileInfo 1]
  711.     foreach cmd [lindex $fileInfo 2] {
  712.         if {$type == "load"} {
  713.         set auto_index($cmd) [list load [file join $dir $f] $pkg]
  714.         } else {
  715.         set auto_index($cmd) [list source [file join $dir $f]]
  716.         } 
  717.     }
  718.     }
  719. }
  720.  
  721. # tclMacPkgSearch --
  722. # The procedure is used on the Macintosh to search a given directory for files
  723. # with a TEXT resource named "pkgIndex".  If it exists it is sourced in to the
  724. # interpreter to setup the package database.
  725.  
  726. proc tclMacPkgSearch {dir} {
  727.     foreach x [glob -nocomplain [file join $dir *.shlb]] {
  728.     if [file isfile $x] {
  729.         set res [resource open $x]
  730.         foreach y [resource list TEXT $res] {
  731.         if {$y == "pkgIndex"} {source -rsrc pkgIndex}
  732.         }
  733.         catch {resource close $res}
  734.     }
  735.     }
  736. }
  737.  
  738. # tclPkgUnknown --
  739. # This procedure provides the default for the "package unknown" function.
  740. # It is invoked when a package that's needed can't be found.  It scans
  741. # the auto_path directories and their immediate children looking for
  742. # pkgIndex.tcl files and sources any such files that are found to setup
  743. # the package database.  (On the Macintosh we also search for pkgIndex
  744. # TEXT resources in all files.)
  745. #
  746. # Arguments:
  747. # name -        Name of desired package.  Not used.
  748. # version -        Version of desired package.  Not used.
  749. # exact -        Either "-exact" or omitted.  Not used.
  750.  
  751. proc tclPkgUnknown {name version {exact {}}} {
  752.     global auto_path tcl_platform env
  753.  
  754.     if ![info exists auto_path] {
  755.     return
  756.     }
  757.     for {set i [expr [llength $auto_path] - 1]} {$i >= 0} {incr i -1} {
  758.     # we can't use glob in safe interps, so enclose the following
  759.     # in a catch statement
  760.     catch {
  761.         foreach file [glob -nocomplain [file join [lindex $auto_path $i] \
  762.             * pkgIndex.tcl]] {
  763.         set dir [file dirname $file]
  764.         if [catch {source $file} msg] {
  765.             tclLog "error reading package index file $file: $msg"
  766.         }
  767.         }
  768.         }
  769.     set dir [lindex $auto_path $i]
  770.     set file [file join $dir pkgIndex.tcl]
  771.     # safe interps usually don't have "file readable", nor stderr channel
  772.     if {[interp issafe] || [file readable $file]} {
  773.         if {[catch {source $file} msg] && ![interp issafe]}  {
  774.         tclLog "error reading package index file $file: $msg"
  775.         }
  776.     }
  777.     # On the Macintosh we also look in the resource fork 
  778.     # of shared libraries
  779.     # We can't use tclMacPkgSearch in safe interps because it uses glob
  780.     if {(![interp issafe]) && ($tcl_platform(platform) == "macintosh")} {
  781.         set dir [lindex $auto_path $i]
  782.         tclMacPkgSearch $dir
  783.         foreach x [glob -nocomplain [file join $dir *]] {
  784.         if [file isdirectory $x] {
  785.             set dir $x
  786.             tclMacPkgSearch $dir
  787.         }
  788.         }
  789.     }
  790.     }
  791. }
  792.